package com.shrimpcolo.johnnytam.FileIO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * 1) 编写一个程序，当用户输入一个目录时，该程序能列出该目录下的所有子目录和文件，并且有层级区分。
 * 
 * @author Johnny Tam
 * 
 *         2) 然后拷贝该文件到指定目录
 */
public class LoopCopyFile {

	private File mSrcFile;
	private File mDestFile;

	LoopCopyFile() {
	}

	LoopCopyFile(File file) {
		this.mSrcFile = file;
	}

	public void loopListFile() {
		if (!mSrcFile.exists()) {
			System.out.println("该文件目录不存在");
		} else {
			loopDocument(mSrcFile, 0);
		}
	}

	public void loopDocument(File file, int level) {

		String preStr = "";
		for (int i = 0; i < level; i++) {
			preStr += "    ";
		}
		File[] childs = file.listFiles();
		if (childs != null) {
			for (int i = 0; i < childs.length; i++) {

				System.out.println(preStr + childs[i].getName());

				if (childs[i].isDirectory()) {
					loopDocument(childs[i], level + 1);
				}
			}
		} // end if

	}

	// 循环拷贝文件
	public void loopCopyFile(File destFile) {
		this.mDestFile = destFile;

		if (!mDestFile.exists()) {
			System.out.println("目标文件目录不存在, 创建目标文件夹！！！");

			mDestFile.mkdirs();
			System.out.println("创建目标文件夹 " + mDestFile.getName() + " 成功！");
		}
		System.out.println("开始复制... ...");

		File[] childs = mSrcFile.listFiles();
		for (int i = 0; i < childs.length; i++) {
			if (childs[i].isFile()) {
				// copy the file
				String path = mDestFile.getAbsolutePath() + File.separator + childs[i].getName();
				File file = new File(path);
				// System.out.println(path);

				try {
					copyFile(childs[i], file);
				} catch (IOException e) {
					e.printStackTrace();
				}

			} else if (childs[i].isDirectory()) {
				// copy the dirs
				String srcPath = mSrcFile.getAbsolutePath() + File.separator + childs[i].getName();
				String dstPath = mDestFile.getAbsolutePath() + File.separator + childs[i].getName();
				// System.out.println(srcPath);
				// System.out.println(dstPath);

				copyDir(srcPath, dstPath);

			}

		}

	}

	// copy the file
	private void copyFile(File sourceFile, File destFile) throws IOException {
		// new file input stream and then buffer it
		FileInputStream fileInput = new FileInputStream(sourceFile);
		BufferedInputStream buffInputStr = new BufferedInputStream(fileInput);

		// new file output stream and then buffer it
		FileOutputStream fileOutput = new FileOutputStream(destFile);
		BufferedOutputStream buffOutputStr = new BufferedOutputStream(fileOutput);

		// v0.1 read one byte on time
		// int byte1;
		// while( (byte1 = buffInputStr.read()) != -1) {
		// buffOutputStr.write(byte1);
		// }

		// v0.2 read one buffer
		// buffer array
		byte[] byteArray = new byte[1024 * 8];
		int len;
		while ((len = buffInputStr.read(byteArray)) != -1) {
			buffOutputStr.write(byteArray, 0, len);
		}
		// flush the bufferd stream
		buffOutputStr.flush();

		// shutdown the streams
		// 关闭流
		buffInputStr.close();
		buffOutputStr.close();
		fileOutput.close();
		fileInput.close();
	}

	// copy the file dirs
	private void copyDir(String srcPath, String dstPath) {

		File dstFile = new File(dstPath);
		File srcFile = new File(srcPath);

		dstFile.mkdirs();

		File[] file = srcFile.listFiles();

		for (int i = 0; i < file.length; i++) {
			if (file[i].isFile()) {

				File sourceFile = file[i];

				String destPath = dstFile.getAbsolutePath() + File.separator + sourceFile.getName();
				File destFile = new File(destPath);

				try {
					copyFile(sourceFile, destFile);
				} catch (IOException e) {
					e.printStackTrace();
				}

			} else if (file[i].isDirectory()) {
				// copy the dirs
				String srcPath1 = srcPath + File.separator + file[i].getName();
				String dstPath1 = dstPath + File.separator + file[i].getName();

				copyDir(srcPath1, dstPath1);
			}

		} // end for

	}

}
